using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Net;
using Microsoft.Xna.Framework.GamerServices;

namespace Zephyris_Space_Mercs
{
    class Network
    {
        private Socket connection;
        private string server;
        private int serverPort;

        private bool error = false;

        public bool Error
        {
            get { return error; }
        }

        public void CreateConnection(string serverAddress, int port)
        {
            error = false;

            this.connection = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            this.server = serverAddress;
            this.serverPort = port;

            var connectionOperation = new SocketAsyncEventArgs { RemoteEndPoint = new DnsEndPoint(this.server, this.serverPort) };
            connectionOperation.Completed += OnConnectionToServerCompleted;

            this.connection.ConnectAsync(connectionOperation);
        }

        public void SendData(string data)
        {
            if (connection != null && !error)
            {
                // convert text to send (prefix with length)
                var message = string.Format("{0};{1}", data.Length, data);
                var buffer = Encoding.UTF8.GetBytes(message);

                // create event args
                var args = new SocketAsyncEventArgs();
                args.RemoteEndPoint = connection.RemoteEndPoint;
                args.Completed += SocketAsyncEventArgs_Completed;
                args.SetBuffer(buffer, 0, buffer.Length);

                this.connection.SendAsync(args);
            }
        }

        private void SocketAsyncEventArgs_Completed(object sender, SocketAsyncEventArgs e)
        {
            // check for errors
            if (e.SocketError != SocketError.Success)
            {
                error = true;
                CleanUp(e);
                return;
            }
            else
            {
                //List<string> MBOPTIONS = new List<string>();
                //MBOPTIONS.Add("OK");
                //Guide.BeginShowMessageBox("Done", "Trade complete!", MBOPTIONS, 0, MessageBoxIcon.Alert, null, null);
                error = false;
            }

        }

        private void OnConnectionToServerCompleted(object sender, SocketAsyncEventArgs e)
        {
            if (e.SocketError != SocketError.Success)
            {
                //List<string> MBOPTIONS = new List<string>();
                //MBOPTIONS.Add("OK");
                //Guide.BeginShowMessageBox("Network Error", "A network communication error has occured", MBOPTIONS, 0, MessageBoxIcon.Error, null, null);

                error = true;
            }
            else
            {
                error = false;
            }
        }

        protected void GetMBResult(IAsyncResult r)
        {
            int? b = Guide.EndShowMessageBox(r);
        }

        private void CleanUp(SocketAsyncEventArgs e)
        {
            if (e.ConnectSocket != null)
            {
                e.ConnectSocket.Shutdown(SocketShutdown.Both);
                e.ConnectSocket.Close();
            }
        }
    }
}
